import json

from django.shortcuts import render
from django.views import View
from pure_pagination.paginator import Paginator
from django.http.response import HttpResponse, JsonResponse

from operation.models import CourseComments, UserFavorite
from .models import Course, CourseResource, Video, Lesson
from operation.models import UserCourse
from users.models import UserProfile


class CoursesListView(View):
    def get(self, request):
        '''
        all_courses={
        'object_list': course_list,
        '':
        }
        :param request:
        :return:
        '''
        sort = request.GET.get('sort')
        page_num = request.GET.get('page', 1)
        if sort == '' or sort is None:
            order = '-add_time'
        elif sort == 'hot':
            order = 'fav_nums'
        else:
            order = 'students'
        course_list = Course.objects.all().order_by(order)
        paginator = Paginator(course_list, 3)
        all_courses = paginator.page(page_num)
        hot_courses = Course.objects.all().order_by('fav_nums')[0:2]
        context = {'all_courses': all_courses,
                   'hot_courses': hot_courses}

        return render(request, 'course-list.html', context)


class CoursesDetailView(View):
    def get(self, request, pk):
        try:
            course = Course.objects.get(pk=pk)
        except:
            return HttpResponse('课程编号错误')
        relate_courses = Course.objects.filter(tag=course.tag)
        user = request.user
        try:
            UserFavorite.objects.get(user_id=user.id, fav_id=course.id, fav_type=1)
        except:
            has_fav_course = False
        else:
            has_fav_course = True
        try:
            UserFavorite.objects.get(user_id=user.id,fav_id=course.course_org_id,fav_type=2)
        except:
            has_fav_org=False
        else:
            has_fav_org=True
        context = {
            'course': course,
            'relate_courses': relate_courses,
            'has_fav_org':has_fav_org,
            'has_fav_course':has_fav_course,
        }
        return render(request, 'course-detail.html', context)


class CourseInfoView(View):
    def get(self, request, pk):
        try:
            course = Course.objects.get(pk=pk)
        except:
            return HttpResponse('课程编号错误')
        # 我要学习  添加到user_courses表中
        user = request.user
        if user.is_authenticated:
            try:
                user = UserCourse.objects.get(user_id=user.id, course_id=course.id)
            except:
                UserCourse.objects.create(user_id=user.id, course_id=course.id)
            else:
                pass

        user_courses = UserCourse.objects.filter(course_id=course.id)
        relate_courses = []
        for user_course in user_courses:
            user = user_course.user
            relate_user_courses = UserCourse.objects.filter(user_id=user.id)
            for relate_user_course in relate_user_courses:
                relate_courses.append(Course.objects.get(pk=relate_user_course.course_id))
        relate_courses_set = set(relate_courses)
        relate_courses = list(relate_courses_set)

        # 渲染页面
        all_comments = CourseComments.objects.filter(course_id=pk)
        all_resources = CourseResource.objects.filter(course_id=pk)
        context = {
            'course': course,
            'all_comments': all_comments,
            'all_resources': all_resources,
            'relate_courses': relate_courses,
        }
        return render(request, 'course-video.html', context)


class CourseCommentView(View):
    def get(self, request, pk):
        try:
            course = Course.objects.get(pk=pk)
        except:
            return HttpResponse('课程编号错误')
        all_comments = CourseComments.objects.filter(course_id=pk)
        all_resources = CourseResource.objects.filter(course_id=pk)
        user_courses = UserCourse.objects.filter(course_id=course.id)
        relate_courses = []
        for user_course in user_courses:
            user = user_course.user
            relate_user_courses = UserCourse.objects.filter(user_id=user.id)
            for relate_user_course in relate_user_courses:
                relate_courses.append(Course.objects.get(pk=relate_user_course.course_id))
        relate_courses_set = set(relate_courses)
        relate_courses = list(relate_courses_set)
        context = {
            'course': course,
            'all_comments': all_comments,
            'all_resources': all_resources,
            'relate_courses': relate_courses,
        }
        return render(request, 'course-comment.html', context)


class AddCommentView(View):
    def post(self, request):
        data_dict = request.POST
        course_id = data_dict.get('course_id')
        comments = data_dict.get('comments')
        user = request.user
        if not user.is_authenticated:
            return JsonResponse({
                'status': 'fail',
                'msg': '用户未登录'
            })
        CourseComments.objects.create(user_id=user.id, course_id=course_id, comments=comments)
        return JsonResponse({'status': 'success'})


class VideoPlayView(View):
    def get(self, request, video_id):
        try:
            video = Video.objects.get(pk=video_id)
        except:
            return HttpResponse('视频编号错误')
        lesson = video.lesson
        course = lesson.course
        user_courses = UserCourse.objects.filter(course_id=course.id)
        relate_courses = []
        for user_course in user_courses:
            user = user_course.user
            relate_user_courses = UserCourse.objects.filter(user_id=user.id)
            for relate_user_course in relate_user_courses:
                relate_courses.append(Course.objects.get(pk=relate_user_course.course_id))
        relate_courses_set = set(relate_courses)
        relate_courses = list(relate_courses_set)

        all_resources = CourseResource.objects.filter(course_id=course.id)
        context = {
            'video': video,
            'course': course,
            'all_resources': all_resources,
            'relate_courses': relate_courses,
        }
        return render(request, 'course-play.html', context)
